package ch.fusun.baron.unit.ui;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.data.DataListener;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.map.Tile;
import ch.fusun.baron.map.ui.gef.editpart.TileChild;
import ch.fusun.baron.map.ui.gef.editpart.TileChildrenProvider;
import ch.fusun.baron.property.api.PropertyService;
import ch.fusun.baron.unit.Unit;
import ch.fusun.baron.unit.service.UnitService;

/**
 * The unit children provider
 */
public class UnitTileChildrenProvider implements TileChildrenProvider {

	private final List<DataListener> listeners = new LinkedList<DataListener>();
	private UnitService unitService;
	private PropertyService propertyService;

	/**
	 * Injection constructor
	 */
	public UnitTileChildrenProvider() {

	}

	/**
	 * @param unitService
	 *            The injected service
	 */
	@Inject
	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
		this.unitService.addDataListener(this);
	}

	/**
	 * @param propertyService
	 *            The injected service
	 */
	@Inject
	public void setPropertyService(PropertyService propertyService) {
		this.propertyService = propertyService;
		this.propertyService.addDataListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Collection<TileChild> getChildren(Tile tile) {
		Collection<TileChild> children = new LinkedList<TileChild>();
		for (Unit unit : unitService.getUnits(tile)) {
			children.add(new UnitTileChild(unit));
		}
		return children;
	}

	@Override
	public void addListener(DataListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void dataChanged(DataUpdate update) {
		for (DataListener listener : listeners) {
			listener.dataChanged(update);
		}
	}

}
